/**
 * 
 */
package com.${ltdAbbr}.xiao.${module}.dal.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.${ltdAbbr}.xiao.${module}.dal.dao.${module?cap_first}Dao;
import com.${ltdAbbr}.xiao.${module}.dal.pojo.${table.name?cap_first}Do;
import com.${ltdAbbr}.xiao.${module}.dal.service.${table.name?cap_first}Service;
import com.yt.xiao.commons.pojo.QueryPageParam;
import com.yt.xiao.commons.pojo.QueryResult;
import com.yt.xiao.commons.pojo.UpdateResult;
import com.yt.xiao.commons.util.XYValidator;
import com.yt.xiao.commons.util.XYValidator.CodeEntity;
import com.yt.xiao.database.dao.XYAbstractDao;

/**
 * ${table.comment}dal service
 * 
 * @author ${author}
 * @version ${curTime}
 */
@Component("${module}_${table.name?uncap_first}DalService")
public class ${table.name?cap_first}ServiceImpl implements ${table.name?cap_first}Service {

    /**
     * mybatis 命名空间 ${table.comment}
     */
    private static final String NAMESPACE = "com.${ltdAbbr}.xiao.${module}.dal.pojo.${table.name?cap_first}Do";

    @Autowired
    @Qualifier(value="${module}Dao")
    private ${module?cap_first}Dao ${module}Dao;

    /**
     * 保存 ${table.comment}
     * @param ${table.name?uncap_first}
     * @return
     */
    @Override
    public UpdateResult save(${table.name?cap_first}Do ${table.name?uncap_first}) {
        UpdateResult result;
        if(${table.name?uncap_first}==null) {
            result = new UpdateResult(-30001, "${table.name?uncap_first} is null");
        } else {
            XYValidator validator = new XYValidator(false);<#list table.columnForAddList as col>
            validator.validateNull(${table.name?uncap_first}.get${col.name?cap_first}(), -${col?index*2+31103}, "${col.name?uncap_first} is null");
            //validator.validateNull(${table.name?uncap_first}.get${col.name?cap_first}(), -${col?index*2+31104}, "${col.name?uncap_first} is null");</#list>
            //validator.validatorateEmpty(${table.name?uncap_first}.getXxxx(), errorCode, "xxx is empty");
            CodeEntity errorCode = validator.getFirstErrorCodeObject();
            if(errorCode!=null) {
                result = new UpdateResult(errorCode.getCode(), errorCode.getMessage());
            } else {
                result = new UpdateResult(${module}Dao.insert(NAMESPACE+"."+XYAbstractDao.STATEMENT_SUFFIX_INSERT, ${table.name?uncap_first}));
            }
        }
        return result;
    }

    /**
     * 删除 ${table.comment}
     * @param ${table.name?uncap_first}
     * @return
     */
    @Override
    public UpdateResult delete(${table.name?cap_first}Do ${table.name?uncap_first}) {
        UpdateResult result;
        if(${table.name?uncap_first}==null) {
            result = new UpdateResult(-30001, "${table.name?uncap_first} is null");
        } else {
            XYValidator validator = new XYValidator(false);<#list table.pkColumnList as col>
            validator.validateNull(${table.name?uncap_first}.get${col.name?cap_first}(), -${col?index*2+31101}, "${col.name?uncap_first} is null");
            //validator.validateNull(${table.name?uncap_first}.get${col.name?cap_first}(), -${col?index*2+31102}, "${col.name?uncap_first} is null");</#list>
            //validator.validatorateEmpty(${table.name?uncap_first}.getXxxx(), errorCode, "xxx is empty");
            CodeEntity errorCode = validator.getFirstErrorCodeObject();
            if(errorCode!=null) {
                result = new UpdateResult(errorCode.getCode(), errorCode.getMessage());
            } else {
                result = new UpdateResult(${module}Dao.delete(NAMESPACE+"."+XYAbstractDao.STATEMENT_SUFFIX_DELETE, ${table.name?uncap_first}));
            }
        }
        return result;
    }

    /**
     * 更新 ${table.comment}
     * @param ${table.name?uncap_first}
     * @return
     */
    @Override
    public UpdateResult update(${table.name?cap_first}Do ${table.name?uncap_first}) {
        UpdateResult result;
        if(${table.name?uncap_first}==null) {
            result = new UpdateResult(-30001, "${table.name?uncap_first} is null");
        } else {
            XYValidator validator = new XYValidator(false);<#list table.columnForEditList as col>
            validator.validateNull(${table.name?uncap_first}.get${col.name?cap_first}(), -${col?index*2+31101}, "${col.name?uncap_first} is null");
            //validator.validateNull(${table.name?uncap_first}.get${col.name?cap_first}(), -${col?index*2+31102}, "${col.name?uncap_first} is null");</#list>
            //validator.validatorateEmpty(${table.name?uncap_first}.getXxxx(), errorCode, "xxx is empty");
            CodeEntity errorCode = validator.getFirstErrorCodeObject();
            if(errorCode!=null) {
                result = new UpdateResult(errorCode.getCode(), errorCode.getMessage());
            } else {
                result = new UpdateResult(${module}Dao.update(NAMESPACE+"."+XYAbstractDao.STATEMENT_SUFFIX_UPDATE, ${table.name?uncap_first}));
            }
        }
        return result;
    }

    /**
     * 分页查询 ${table.comment}  列表
     * @param ${table.name?uncap_first}
     * @param queryPageParam
     * @return ${table.comment} list
     */
    @Override
    public QueryResult<${table.name?cap_first}Do> selectList(${table.name?cap_first}Do ${table.name?uncap_first}, QueryPageParam queryPageParam){
        QueryResult<${table.name?cap_first}Do> result;
        if (queryPageParam==null) {
            result = new QueryResult<>(${module}Dao.selectList(NAMESPACE+"."+XYAbstractDao.STATEMENT_SUFFIX_SELECT_LIST, ${table.name?uncap_first}), -1);
        } else {
            result = ${module}Dao.selectListAndCount(NAMESPACE+"."+XYAbstractDao.STATEMENT_SUFFIX_SELECT_LIST, ${table.name?uncap_first}, queryPageParam.getX_pn(), queryPageParam.getX_pc(), queryPageParam.getX_seq());
        }
        return result;
    }

    /**
     * 根据主键查询 ${table.comment}
     * @param ${table.name?uncap_first}
     * @return ${table.comment}
     */
    @Override
    public ${table.name?cap_first}Do find(${table.name?cap_first}Do ${table.name?uncap_first}) {
        ${table.name?cap_first}Do result;
        if(${table.name?uncap_first}==null) {
            result = null;
        } else {
            XYValidator validator = new XYValidator(false);<#list table.pkColumnList as col>
            validator.validateNull(${table.name?uncap_first}.get${col.name?cap_first}(), -${col?index*2+1101}, "${col.name?uncap_first} is null");</#list>
            //validator.validatorateEmpty(${table.name?uncap_first}.getXxxx(), errorCode, "xxx is empty");
            CodeEntity errorCode = validator.getFirstErrorCodeObject();
            if(errorCode!=null) {
                result = null;
            } else {
                List<${table.name?cap_first}Do> list = ${module}Dao.selectList(NAMESPACE+"."+XYAbstractDao.STATEMENT_SUFFIX_SELECT_LIST, ${table.name?uncap_first});
                result = list!=null && list.size()>0 ? list.get(0) : null;
            }
        }
        return result;
    }
}
